# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=intel-xed
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2025.03.02
_mbuild_ver=2024.11.04
pkgrel=1
pkgdesc="Intel X86 Encoder Decoder (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:Apache-2.0')
url="https://github.com/intelxed/xed/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-yasm"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://github.com/intelxed/xed/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/intelxed/mbuild/archive/v${_mbuild_ver}/mbuild-${_mbuild_ver}.tar.gz"
        001-mbuild-shared.patch
        002-xed-build.patch
        003-mbuild-remove-m64.patch
        004-aarch64.patch
        XEDConfig.cmake
        XEDConfigVersion.cmake
        XEDTargets.cmake
        XEDTargets-release.cmake)
sha256sums=('2f43dbe833fd6d3475d3842e1f4530e6aa084cae89af1a7e06642ceb0cad1a0a'
            '866d17a3aa3b3f4d8d7157d747d54b13b4300c9fd5bf15ad9289b1b3309e0b71'
            'de8a938a30c2e18e922c7deb2349c454470781d1f5a8b01b7fefb4d45504ea32'
            '2b0f746faa343d078815be96f1dce10f3625946d862a0213792a192a0a4957f0'
            '994db2654d484c7da80075a01743d45c7e79c7800ae4be8bff2e481b0ce6baf7'
            '139c11d97c30a84554f45fa183976c6d2aca7119e61aaead366277534138ceb5'
            '5ce572ebc6acabe4f18affc9dbdbd045dd81e4af6b99ac9bbdbe4b5bb0113ab0'
            '5b67da254e4a1c7b2a05d0d2eeb60d8f9767f0179326e2c6f6c59f850ab28dc3'
            '9ccd6f8b79e51f9c9318969af3464466fe5060e7ab06cb37993228cb7d69a090'
            '560c662524317214e80575279f09fff05612e389345ee294c21539c3fade4068')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"

  cd "${srcdir}/xed-${pkgver}"
  apply_patch_with_msg \
    002-xed-build.patch

  mv "${srcdir}"/mbuild-${_mbuild_ver} "${srcdir}"/mbuild && cd "${srcdir}"/mbuild
  apply_patch_with_msg \
    001-mbuild-shared.patch \
    003-mbuild-remove-m64.patch \
    004-aarch64.patch
}

build() {
  cd "${srcdir}/xed-${pkgver}"

  ${MINGW_PREFIX}/bin/python.exe ./mfile.py --shared --opt=2 --compiler=clang --jobs=$(nproc)
}

package() {
  cd "${srcdir}"/xed-${pkgver}
  ${MINGW_PREFIX}/bin/python.exe ./mfile.py --shared --opt=2 --compiler=clang --prefix="${pkgdir}"${MINGW_PREFIX} install

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/bin
  mv "${pkgdir}"${MINGW_PREFIX}/lib/*.dll "${pkgdir}"${MINGW_PREFIX}/bin/

  cp -f "${srcdir}"/xed-${pkgver}/*.dll.a "${pkgdir}"${MINGW_PREFIX}/lib/

  # License
  install -Dm644 "${srcdir}/xed-${pkgver}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  install -Dm644 "${srcdir}/XEDConfig.cmake" "${pkgdir}${MINGW_PREFIX}/lib/cmake/xed/XEDConfig.cmake"
  install -Dm644 "${srcdir}/XEDConfigVersion.cmake" "${pkgdir}${MINGW_PREFIX}/lib/cmake/xed/XEDConfigVersion.cmake"
  install -Dm644 "${srcdir}/XEDTargets.cmake" "${pkgdir}${MINGW_PREFIX}/lib/cmake/xed/XEDTargets.cmake"
  install -Dm644 "${srcdir}/XEDTargets-release.cmake" "${pkgdir}${MINGW_PREFIX}/lib/cmake/xed/XEDTargets-release.cmake"

  sed -i "s#@XED_VERSION@#${pkgver}#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/xed/XEDConfig.cmake

  case "${CARCH}" in
    x86_64|aarch64)
      sed -i "s#@@VOID_SIZE@@#8#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/xed/XEDConfigVersion.cmake
      ;;
    armv7|i?86)
      sed -i "s#@@VOID_SIZE@@#4#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/xed/XEDConfigVersion.cmake
      ;;
  esac
}
